

package jdd.internal.tutorial;

import jdd.util.*;

import java.io.*;
import java.lang.reflect.*;

class TutorialHelper {
	/* package */ static final String base = "tutorial/";
	/* package */ String tutorial, index, file;
	protected TutorialHelper(String name) { this(name, name); }
	protected TutorialHelper(String name, String file) {
		System.out.println("\t\t" + name);
		try {
			this.tutorial = name;
			this.file = file;
			this.index = file + ".html";
			JDDConsole.out = new FileTarget(base + index);
			Dot.setType(Dot.TYPE_PNG);
			JDDConsole.out.println("<html>");
			JDDConsole.out.println("<link rel=\"stylesheet\" href=\"../bdd.css\">");
			JDDConsole.out.println("<br><font size=-1>(this file is automatically generated from Java source)</font><br>");
			JDDConsole.out.println("<a href=index.html>Back</a> to book index.<hr><p>");


		} catch(IOException exx) {
			exx.printStackTrace();
			System.exit(20);
		}
	}

	protected void finalize() { // this will probably never get called by the stupid JVM :(
		JDDConsole.out.println("<br><br><hr>");
		JDDConsole.out.println("<a href=index.html>Back</a> to book index.");
		JDDConsole.out.println("</html>");
	}


	/* package */ void img(String name) {
		JDDConsole.out.println("<center><img src=\""  + file + name + ".png\" ></center>");
	}

	/* package */ void h3(String text) {
		JDDConsole.out.println("<h3>" + text + "</h3>");
	}

	/* package */ void h2(String text) {
		JDDConsole.out.println("<h2>" + text + "</h2>");
	}

	/* package */ void href(String url, String name) {
		JDDConsole.out.println("< a target=_blank href=\"" + url + "\">" + name + "</a>");
	}

	/* package */ void br() {
		JDDConsole.out.println("<br>");
	}
	/* package */ String filename(String name) {
		return base + file + name;
	}
	/* package */ void code(String code_) {
		JDDConsole.out.print("<ul><TABLE BORDER=0 CELLPADDING=1 CELLSPACING=1><TR><TD BGCOLOR=#FFFF40>");
		JDDConsole.out.print("<pre>"+ code_ + "</pre>");
		JDDConsole.out.println("</TD></tr></table></ul>");

	}
	/* package */ void showClass(String cls) {
		try {
			Class klass = Class.forName(cls);
			JDDConsole.out.println("<ul><TABLE BORDER=1 CELLPADDING=1 CELLSPACING=1><TR><TD COLSPAN=3 BGCOLOR=#EEEEFF>");
			JDDConsole.out.println("Class " + klass.getName()  );
			JDDConsole.out.println("</TD></tr>");
			JDDConsole.out.println("<TR BGCOLOR=#EEEE00><td>return type</TD><TD>method</TD><TD>parameters</TD></TR>");

			Method [] methods = klass.getDeclaredMethods();
			for(int i = 0; i < methods.length; i++) {
				String name = methods[i].getName();
				if(!name.equals("main") && !name.equals("cleanup") &&
				(methods[i].getModifiers() & Modifier.PUBLIC) != 0)  {
					JDDConsole.out.println("<TR><TD>");
					printClassName(methods[i].getReturnType());
					JDDConsole.out.println("</TD><TD>");
					JDDConsole.out.println("<b>" + methods[i].getName() +"</b></TD>");

					Class [] pars = methods[i].getParameterTypes();
					if(pars.length > 0) {
						JDDConsole.out.println("<TD>(");
						if(pars.length > 3) JDDConsole.out.print("<font size=-2>");
						printClassNames(pars);
						if(pars.length > 3) JDDConsole.out.print("</font>");
						JDDConsole.out.println(" )</TD>");
					}
					JDDConsole.out.println("</TR>");
				}
			}

			Field[] fields = klass.getDeclaredFields();
			if(fields.length > 0) {
				int got = 0;
				for(int i = 0; i < fields.length; i++) {
					if((fields[i].getModifiers() & Modifier.PUBLIC) != 0)  {
						if(got == 0) JDDConsole.out.println("<TR BGCOLOR=#EEEE00><td>Field</TD><TD>type</TD></TR>" );
						got++;
						JDDConsole.out.println("<TR><TD>");
						JDDConsole.out.println("Field <b>"+ fields[i].getName()+"</b></TD><TD>" + fields[i].toString());
						JDDConsole.out.println("</TD></TR>");
					}
				}
			}
			JDDConsole.out.println("</TABLE></ul>");
		} catch(ClassNotFoundException exx) {
			JDDConsole.out.println("Class not found: " + cls);
			System.exit(20);
		}
	}

	private void printClassNames(Class [] c) {
		for(int i = 0; i < c.length; i++) {
			if(i != 0) JDDConsole.out.print(",  ");
			printClassName(c[i]);
		}
	}
	private void printClassName(Class c) {
		String name = c.getName();

		if(name.equals("[B")) name = "byte";
		else if(name.equals("[C")) name = "char";
		else if(name.equals("[D")) name = "double";
		else if(name.equals("[F")) name = "float";
		else if(name.equals("[I")) name = "int";
		else if(name.equals("[J")) name = "long";
		else if(name.equals("[Z")) name = "boolean";
		else if(name.equals("[V")) name = "void";

		JDDConsole.out.print(name );

		if(c.isArray()) JDDConsole.out.print("[]");
	}
}
